﻿using STHPMS1.BLL.B_Core;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Serv;
using System.Data;
using STHPMS1.Common.C_Enum;
using STHPMS1.Common.C_Extension;

namespace STHPMS1.BLL.B_Bus
{
    public class Dr_Decision_TreeBus : BllBase, IDr_Decision_TreeBill
    {
        public void Save<T>(T entity)
            where T : class ,IDr_Decision_Tree
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IDr_Decision_Tree entity)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            using (IDbTransaction tran = dao.BeginTransaction())
            {
                dao.Delete<Dr_Lifecyclecost>(p => p.Proj_Id == entity.Proj_Id);
                dao.Delete<Dr_Repaircost>(p => p.Proj_Id == entity.Proj_Id);
                dao.Delete<Dr_Initialconstruction>(p => p.Proj_Id == entity.Proj_Id);
                dao.DeleteById<Dr_Decision_Tree>(entity.Proj_Id);
                tran.Commit();
            }
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IDr_Decision_Tree
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }


        public void SavePlan(string schmeName, string mainSegId, string[] iniproj_Ids)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;

            Dr_Decision_Tree plan = new Dr_Decision_Tree();
            plan.Mainsegid = mainSegId;
            plan.Schme_Name = schmeName;
            plan.Design_Time = DateTime.Now;

            List<Dr_Initialconstruction> costList = new List<Dr_Initialconstruction>();
            IEnumerable<Dr_Stru_Mate> mateList = dao.Select<Dr_Stru_Mate>();
            if (mateList == null || mateList.Count() == 0) throw new Exception("材料库的内容为空!");

            Dn_Divi_Result seg=dao.Single<Dn_Divi_Result>(p=>p.Mainsegid==mainSegId);
            if(seg==null) throw new Exception("路段信息不存在 - "+mainSegId);
            if (seg.Surfwidth == null || seg.Surfwidth == 0) throw new Exception("路段的路面宽度不存在 -" + mainSegId);
            if (seg.Endmp == null || seg.Beginmp == null) throw new Exception("路段的起终点桩号为空-" + mainSegId);
            if (seg.Rdclass_Dictid == null) throw new Exception("路段的公路等级为空-" + mainSegId);

            double length = (double)(seg.Endmp - seg.Beginmp);//路面长度
            if (length==0) throw new Exception("路段的长度为0 - " + mainSegId);

            plan.Proj_Id = MyApp.Locator.GetInstance < IKeyGenrateService>().NewPlanId(seg.Rdwayid,seg.Beginmp.ToString(),seg.Direction_Dictid.ToString(),(DateTime)plan.Design_Time ,1);
            //系统通过路段的技术等级内置默认值 高速、一级公路：15年，二级公路：12年，三级公路：8年，四级公路：6年。
            switch(seg.Rdclass_Dictid)
            {
                case (int)RoadClassEnum.R160:
                case (int)RoadClassEnum.R161:                
                    {
                        plan.Analy_Period = 15;
                        break;
                    }
                case (int)RoadClassEnum.R162:
                    {
                        plan.Analy_Period = 12;
                        break;
                    }
                case (int)RoadClassEnum.R163:
                    {
                        plan.Analy_Period = 8;
                        break;
                    }
                case (int)RoadClassEnum.R164:
                    {
                        plan.Analy_Period = 6;
                        break;
                    }

            }

            foreach (var iniproj_id in iniproj_Ids)
            {
                Dr_Initialconstruction cost = new Dr_Initialconstruction();
                cost.Proj_Id = plan.Proj_Id;
                cost.Iniproj_Id = iniproj_id;

                IEnumerable<Dr_Ini_Laymate> layList = dao.Select<Dr_Ini_Laymate>(p => p.Iniproj_Id == iniproj_id);
                if (layList == null || layList.Count() == 0) throw new Exception("初选方案的材料为空 -"+iniproj_id);

                double amount=1000*(double)seg.Surfwidth;//工程量
                double sum = 0d;//初期建设费用

                foreach (var item in layList)
	            {
                    Dr_Stru_Mate mate=mateList.FirstOrDefault(p=>p.Stru_Id==item.Stru_Id);
                    if(mate==null)throw new Exception("材料在库中不存在 - "+item.Stru_Id);
                    sum += (double)(item.Mater_Thick * mate.Stru_Price * amount * length);//厚度*单价*工程量*长度=初期建设费用
	            }

                cost.Amount = amount;
                cost.Inicons_Cost = sum;

                costList.Add(cost);
            }

            using (IDbTransaction dbTrans = dao.BeginTransaction())
            {
                dao.Save<Dr_Decision_Tree>(plan);
                dao.SaveAll<Dr_Initialconstruction>(costList);
                dbTrans.Commit();
            }
        }
    }
}
